﻿using System;
using System.Collections.Generic;

using System.Text;
using Newtonsoft.Json;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Converters;

namespace Help
{
    public static class ReturnJsonHelp
    {

        private static JsonSerializerSettings _jsonSettings;



        static ReturnJsonHelp()
        {

            IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverter();

            datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";



            _jsonSettings = new JsonSerializerSettings();

            _jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;

            _jsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

            _jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

            _jsonSettings.Converters.Add(datetimeConverter);

        }


        /// <summary>  

        /// 将指定的对象序列化成 JSON 数据。  

        /// </summary>  

        /// <param name="obj">要序列化的对象。</param>  

        /// <returns></returns>  

        public static string ToJson(this object obj)
        {

            try
            {

                if (null == obj)

                    return null;



                return JsonConvert.SerializeObject(obj, Formatting.None, _jsonSettings);

            }

            catch
            {

                return null;

            }

        }



        /// <summary>  

        /// 将指定的 JSON 数据反序列化成指定对象。  

        /// </summary>  

        /// <typeparam name="T">对象类型。</typeparam>  

        /// <param name="json">JSON 数据。</param>  

        /// <returns></returns>  

        public static T FromJson<T>(this string json)
        {

            try
            {

                return JsonConvert.DeserializeObject<T>(json, _jsonSettings);

            }

            catch (Exception)
            {

                return default(T);

            }

        }

        
        /// <summary>
        /// 一般的json字符串
        /// </summary>
        /// <param name="isError">是否错误  true,false</param>
        /// <param name="Message">抛出的信息</param>
        /// <returns></returns>
        public static string OutHandlerJson(bool isError, string message)
        {
            var outJson = new { IsError = isError, Message = message };
            return JsonConvert.SerializeObject(outJson);
        }

        public static string OutHandlerJson(bool isError, string key, string message)
        {
            var outJson = new { IsError = isError, Key = key, Message = message };
            return JsonConvert.SerializeObject(outJson);
        }

        /// <summary>
        /// 获取BJUI返回值JSON字符串
        /// </summary>
        /// <param name="statusCode">必选。状态码(ok = 200, error = 300, timeout = 301)</param>
        /// <param name="message">可选。信息内容。</param>
        /// <param name="tabid">可选。待刷新navtab id，多个id以英文逗号分隔开，当前的navtab id不需要填写，填写后可能会导致当前navtab重复刷新。</param>
        /// <param name="dialogid">可选。待刷新dialog id，多个id以英文逗号分隔开，请不要填写当前的dialog id，要控制刷新当前dialog，请设置dialog中表单的reload参数。</param>
        /// <param name="divid">可选。待刷新div id，多个id以英文逗号分隔开，请不要填写当前的div id，要控制刷新当前div，请设置该div中表单的reload参数。</param>
        /// <param name="closeCurrent">可选。是否关闭当前窗口(navtab或dialog)。</param>
        /// <param name="forward">可选。跳转到某个url。</param>
        /// <param name="forwardConfirm">可选。跳转url前的确认提示信息。</param>
        /// <returns></returns>
        public static string BJUIajaxJson(int statusCode, string message)
        {
            var outJson = new { statusCode = statusCode, message = message };
            return JsonConvert.SerializeObject(outJson);
        }
        public static string BJUIajaxJsonForUpload(int statusCode, string message, string fileName)
        {
            var outJson = new { statusCode = statusCode, message = message, fileName = fileName };
            return JsonConvert.SerializeObject(outJson);
        }
        public static string BJUIajaxJson(int statusCode, string message, string tabid,string dialogid,string divid, bool closeCurrent, string forward, string forwardConfirm)
        {
            var outJson = new { statusCode = statusCode, message = message, tabid = tabid, dialogid = dialogid, divid = divid, closeCurrent = closeCurrent, forward = forward, forwardConfirm = forwardConfirm };
            return JsonConvert.SerializeObject(outJson);
        }
    }
}
